<div id="company_form_content" class="wide form">
<p>
Fields with <span class="required">*</span> are required.
</p>
<?php $this->breadcrumbs = '';?>
<?php echo CHtml::statefulForm(); ?>
 <fieldset class="ui-widget section">
        <legend class="ui-widget-header ui-corner-all">Informations</legend>
<div class="row">
<?php echo CHtml::activeLabelEx($models['company'],'cname'); ?>
<?php echo CHtml::activeTextField($models['company'], 'cname',array('size'=>40));?>
</div>
<?php $this->renderPartial('_address', array('address'=>$models['company_address'][0],'belong'=>0)) ?>
 </fieldset>
         <fieldset class="ui-widget section">
        <legend class="ui-widget-header ui-corner-all">Additional Info</legend>
<?php
$address = '';
foreach($models['company_address'] as $i=>$item) {
    if($i>0)
        $address .= $this->renderPartial('_address',array('address'=>$models['company_address'][$i],'belong'=>$i),true);
}
$members = $this->renderPartial('_members', array('member'=>$models['company_members'][0],'belong'=>0), true);
$this->widget('zii.widgets.jui.CJuiTabs', array(
        'id'=>'company_tab',
        'tabs'=>array(
            'Addresses'=>array('id'=>'tabaddress','content'=>$address),
            'Employee'=>array('id'=>'tabmember','content'=>$members),
        ),
    )
);
?>
         </fieldset>
<?php //echo CHtml::errorSummary($models); ?>

<div class="action">
<?php echo CHtml::submitButton($update ? 'Save' : 'New Customer'); ?>
</div>

<?php echo CHtml::endForm(); ?>
</div><!-- yiiForm -->
<?php
$cs = Yii::app()->getClientScript();
$script =<<<EOD
$('input[type="submit"], input[type="button"]').button();
$('div#telephone input, div#fax input, div#email input').live('keypress',function()
    {
        g=false;
        container = $(this).parent('div').parent('div');
        $(container).children('div:last').children('input').each(function()
            {
                if($(this).val() != "")
                {
                    g=true;
                }
            }
        );
        if(g==true)
        {
            contacts = $(container).parent('div').parent('div');
            var i = $(container).parent('div').parent('div').data('index')+1;//$(contacts).children('div').children('div#telephone').children('div').length + $(contacts).children('div').children('div#fax').children('div').length + $(contacts).children('div').children('div#email').children('div').length;
            $(container).parent('div').parent('div').data('index',i);
            $(container).children('div:last').clone().appendTo(container);
            $(container).children('div:last').children('input, select').each(function(){
                var name = $(this).attr("name").match(/\[[0-9]+\]/gi);
                var oldName='',newName = '';
                for(g=0;g<name.length;g++) {
                    oldName += name[g];
                }
                name[1] = "["+i+"]";
                for(g=0;g<name.length;g++) {
                    newName += name[g];
                }
                $(this).attr('name',$(this).attr('name').replace(oldName,newName));
                //$(this).parent('div').children('select').attr('name',$(this).parent('div').children('select').attr('name').replace(oldName,newName));
            });
            $(container).children('div:last').children('input').val('');            
        }
    }
);
$('div#telephone input, div#fax input').live('keypress',function(evt)
    {
        var charCode = (evt.which) ? evt.which : event.keyCode
       if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
            return false
        }
    }

);
$('div#tabaddress input, div#tabmember input').live('keypress',function()
    {
        g=false;
        container = $(this).parent('div').parent('div').parent('div').parent('div');
        $(container).not('#contacts, #email').children('div:last').children('div').children('div').children('input').each(function()
            {
                if($(this).val() != "")
                {
                    g=true;
                }
            }
        );
        if(g==true)
        {
            var i=$(container).data('index') + 1;
            $(container).data('index',i);
            $(container).append("<hr />");
            $(container).children('div:last').clone().appendTo(container);
            $(container).children('div:last').html($(container).children('div:last').html().replace(/\[[0-9]+\]/gi,'['+i+']'));
            $(container).children('div:last').children('div').children('div').children('div#telephone').children('div:gt(0)').remove();
            $(container).children('div:last').children('div').children('div').children('div#fax').children('div:gt(0)').remove();
            $(container).children('div:last').children('div').children('div').children('div#email').children('div:gt(0)').remove();
            $(container).children('div:last').children('div#contacts').data('index',1);
            $(container).children('div:last').children('div#email').data('index',0);
            $(container).children('div:last').children('div').children('div').children('div#telephone, div#email, div#fax').children('div').children('select, input').each(function() {
                var name = $(this).attr("name").match(/\[[0-9]+\]/gi);
                var oldName='',newName = '';
                for(g=0;g<name.length;g++) {
                    oldName += name[g];
                }
                name[1] = "[0]";
                for(g=0;g<name.length;g++) {
                    newName += name[g];
                }
                $(this).attr('name',$(this).attr('name').replace(oldName,newName));
            });
            $(container).children('div:last').children('div').children('div').children('div#fax').children('div').children('select, input').each(function() {
                var name = $(this).attr("name").match(/\[[0-9]+\]/gi);
                var oldName='',newName = '';
                for(g=0;g<name.length;g++) {
                    oldName += name[g];
                }
                name[1] = "[1]";
                for(g=0;g<name.length;g++) {
                    newName += name[g];
                }
                $(this).attr('name',$(this).attr('name').replace(oldName,newName));
            });
            $(container).children('div:last').children('div').children('div').children('div#email').children('div').children('select, input').each(function() {
                var name = $(this).attr("name").match(/\[[0-9]+\]/gi);
                var oldName='',newName = '';
                for(g=0;g<name.length;g++) {
                    oldName += name[g];
                }
                name[1] = "[2]";
                for(g=0;g<name.length;g++) {
                    newName += name[g];
                }
                $(this).attr('name',$(this).attr('name').replace(oldName,newName));
            });

        }
    }
);
EOD;
$cs->registerScript('company#test',$script);
?>